/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */
package com.nai365.nart.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nai365.nart.common.security.util.SecurityUtils;
import com.nai365.nart.operation.entity.CompoundDemand;
import com.nai365.nart.operation.entity.CompoundResources;
import com.nai365.nart.operation.mapper.CompoundDemandMapper;
import com.nai365.nart.operation.service.CompoundDemandService;
import com.nai365.nart.operation.service.CompoundResourcesService;
import com.nai365.nart.operation.service.ProductService;
import com.nai365.nart.product.entity.SkuInfo;
import com.nai365.nart.product.entity.SpuInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 合成需求
 *
 * @author baogs
 * @date 2022-07-01 11:13:17
 */
@Service
@AllArgsConstructor
@Transactional
public class CompoundDemandServiceImpl extends ServiceImpl<CompoundDemandMapper, CompoundDemand> implements CompoundDemandService {

	private final CompoundResourcesService compoundResourcesService;

	private final ProductService productService;


	@Override
	@Transactional
	public boolean save(CompoundDemand compoundDemand) {
		SpuInfo spuInfo = this.productService.findSpuById(compoundDemand.getTargetSpuId());
		compoundDemand.setCreateBy(SecurityUtils.getUser().getId());
		compoundDemand.setId(null);
		if (spuInfo != null) {
			compoundDemand.setTargetSpuName(spuInfo.getSpuName());
			compoundDemand.setTargetSpuToken(spuInfo.getToken() + "");
			compoundDemand.setTargetSpuImage(spuInfo.getChainUrl());
		}
		super.save(compoundDemand);
		for (CompoundResources compoundResources : compoundDemand.getResourcesList()) {
			compoundResources.setId(null);
			compoundResources.setCompoundDemandId(compoundDemand.getId());
			spuInfo = this.productService.findSpuById(compoundDemand.getTargetSpuId());
			if (spuInfo != null) {
				compoundResources.setSpuToken(spuInfo.getToken() + "");
				compoundResources.setSpuImage(spuInfo.getChainUrl());
			}
			this.compoundResourcesService.save(compoundResources);
		}
		return true;
	}

	@Override
	public CompoundDemand getById(Serializable id) {
		CompoundDemand compoundDemand = super.getById(id);
		LambdaQueryWrapper<CompoundResources> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(CompoundResources::getCompoundDemandId, compoundDemand.getId());
		compoundDemand.setResourcesList(compoundResourcesService.list(queryWrapper));
		return compoundDemand;
	}

	@Override
	public boolean updateById(CompoundDemand compoundDemand) {
		//保存合成数据
		SpuInfo spuInfo = this.productService.findSpuById(compoundDemand.getTargetSpuId());
		compoundDemand.setCreateBy(SecurityUtils.getUser().getId());
		if (spuInfo != null) {
			compoundDemand.setTargetSpuName(spuInfo.getSpuName());
			compoundDemand.setTargetSpuToken(spuInfo.getToken() + "");
			compoundDemand.setTargetSpuImage(spuInfo.getChainUrl());
		}
		super.updateById(compoundDemand);
		//删除资源数据
		LambdaQueryWrapper<CompoundResources> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(CompoundResources::getCompoundDemandId, compoundDemand.getId());
		List<CompoundResources> resourcesList = compoundResourcesService.list(queryWrapper);
		if (resourcesList != null && !resourcesList.isEmpty()) {
			Set<Long> idList = new HashSet<>();
			for (CompoundResources resources : resourcesList) {
				idList.add(resources.getId());
			}
			this.compoundResourcesService.removeBatchByIds(idList);
		}
		//添加资源数据
		for (CompoundResources compoundResources : compoundDemand.getResourcesList()) {
			compoundResources.setId(null);
			compoundResources.setCompoundDemandId(compoundDemand.getId());
			spuInfo = this.productService.findSpuById(compoundDemand.getTargetSpuId());
			if (spuInfo != null) {
				compoundResources.setSpuToken(spuInfo.getToken() + "");
				compoundResources.setSpuImage(spuInfo.getChainUrl());
			}
			this.compoundResourcesService.save(compoundResources);
		}
		return true;
	}

	@Override
	public boolean removeById(CompoundDemand entity) {
		LambdaQueryWrapper<CompoundResources> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(CompoundResources::getCompoundDemandId, entity.getId());
		List<CompoundResources> resourcesList = compoundResourcesService.list(queryWrapper);
		if (resourcesList != null && !resourcesList.isEmpty()) {
			Set<Long> idList = new HashSet<>();
			for (CompoundResources resources : resourcesList) {
				idList.add(resources.getId());
			}
			this.compoundResourcesService.removeBatchByIds(idList);
		}
		return super.removeById(entity);
	}

	@Override
	public CompoundDemand getAbleCountById(Long id) {
		CompoundDemand compoundDemand = super.getById(id);
		LambdaQueryWrapper<CompoundResources> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(CompoundResources::getCompoundDemandId, compoundDemand.getId());
		List<CompoundResources> resourcesList = compoundResourcesService.list(queryWrapper);
		if (resourcesList != null && !resourcesList.isEmpty()) {
			for (CompoundResources resources : resourcesList) {
				List<SkuInfo> skuInfoList = this.productService.findMySkuBySpuIdAndStatus(resources.getSpuId(), 0);
				resources.setAbleCopies(skuInfoList.size());
				resources.setAbleSkuList(skuInfoList);
			}
		}
		compoundDemand.setResourcesList(resourcesList);
		return compoundDemand;
	}

	@Override
	public List<CompoundResources> findResourcesByDemand(Long demand) {
		LambdaQueryWrapper<CompoundResources> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(CompoundResources::getCompoundDemandId, demand);
		return compoundResourcesService.list(queryWrapper);
	}
}
